using System;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TMFileManager {
    public partial class CopyFile : Form {
        private bool move;

        public string SourceFile {
            get {
                return txtSrc.Text;
            }
            set {
                txtSrc.Text = value;
            }
        }

        public string DestinationFile {
            get {
                return txtDst.Text;
            }
            set {
                txtDst.Text = value;
            }
        }

        public CopyFile(bool move) : this() {
            this.move = move;
            Text = "Move file dialog";
        }

        public CopyFile() {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e) {
            if (File.Exists(txtSrc.Text)) {
                if (File.Exists(txtDst.Text)) {
                    if (MessageBox.Show("Are you sure you want to overwrite the file " + txtDst.Text + " ?", "Copy File Dialog", MessageBoxButtons.YesNo,
                        MessageBoxIcon.Warning) == DialogResult.Yes) {
                        Cursor c = Cursor;
                        Cursor = Cursors.WaitCursor;
                        if (move) {
                            File.Delete(txtDst.Text);
                            File.Move(txtSrc.Text, txtDst.Text);
                        } else {
                            File.Copy(txtSrc.Text, txtDst.Text, true);
                        }
                        Cursor = c;
                    } else {
                        DialogResult = DialogResult.Cancel;
                    }
                } else {
                    Cursor c = Cursor;
                    Cursor = Cursors.WaitCursor;
                    if (move) {
                        File.Move(txtSrc.Text, txtDst.Text);
                    } else {
                        File.Copy(txtSrc.Text, txtDst.Text, true);
                    }
                    Cursor = c;
                }
            } else if (Directory.Exists(txtSrc.Text)) {
                if (Directory.Exists(txtDst.Text)) {
                    if (MessageBox.Show("Are you sure you want to overwrite the directory " + txtDst.Text + " ?", "Copy File Dialog",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) {
                        Cursor c = Cursor;
                        Cursor = Cursors.WaitCursor;
                        if (move) {
                            Directory.Delete(txtDst.Text, true);
                            Directory.Move(txtSrc.Text, txtDst.Text);
                        } else {
                            Util.CopyDirectory(txtSrc.Text, txtDst.Text, true);
                        }
                        Cursor = c;
                    } else {
                        DialogResult = DialogResult.Cancel;
                    }
                } else {
                    Cursor c = Cursor;
                    Cursor = Cursors.WaitCursor;
                    if (move) {
                        Directory.Move(txtSrc.Text, txtDst.Text);
                    } else {
                        Util.CopyDirectory(txtSrc.Text, txtDst.Text, true);
                    }
                    Cursor = c;
                }
            }
        }
    }
}